package com.cskaoyan.service;

import com.cskaoyan.bean.vo.AddressVo;
import com.cskaoyan.domain.MarketAddress;
import com.cskaoyan.domain.MarketAddressExample;
import com.cskaoyan.domain.MarketUser;
import com.cskaoyan.mapper.MarketAddressMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService{
    @Autowired
    MarketAddressMapper addressMapper;
    @Override
    public List<MarketAddress> selectAllAddress() {
        MarketAddressExample example = new MarketAddressExample();
        return addressMapper.selectByExample(example);
    }

    @Override
    @Transactional
    public void insertAddress(AddressVo addressVo) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId=0;
        if (subject.isAuthenticated()) {
            MarketUser principal = (MarketUser) subject.getPrincipal();
            userId = principal.getId();
        }
        if (addressVo.isDefaultAddress()) {
            MarketAddressExample example = new MarketAddressExample();
            List<MarketAddress> addressList = addressMapper.selectByExample(example);
            for (MarketAddress address : addressList) {
                if (address.getIsDefault()) {
                    address.setIsDefault(false);
                    addressMapper.updateByPrimaryKey(address);
                }
            }
        }
        MarketAddress address = new MarketAddress();
        address.setName(addressVo.getName());
        address.setUserId(userId);
        address.setProvince(addressVo.getProvince());
        address.setCity(addressVo.getCity());
        address.setCounty(addressVo.getCounty());
        address.setAddressDetail(addressVo.getAddressDetail());
        address.setAreaCode(addressVo.getAreaCode());
        address.setTel(addressVo.getTel());
        address.setIsDefault(addressVo.isDefaultAddress());
        addressMapper.insertSelective(address);
    }

    @Override
    public MarketAddress selectOneAddressByTel(String tel) {
        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        criteria.andTelEqualTo(tel);
        return addressMapper.selectOneByExample(example);
    }

    @Override
    @Transactional
    public void deleteAddressById(Integer id) {
        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        addressMapper.deleteByExample(example);
    }

    @Override
    public MarketAddress selectAddressById(Integer id) {
        return addressMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void updateAddressById(AddressVo addressVo) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId=0;
        if (subject.isAuthenticated()) {
            MarketUser principal = (MarketUser) subject.getPrincipal();
            userId = principal.getId();
        }
        if (addressVo.isDefaultAddress()) {
            MarketAddressExample example = new MarketAddressExample();
            List<MarketAddress> addressList = addressMapper.selectByExample(example);
            for (MarketAddress address : addressList) {
                if (address.getIsDefault()) {
                    address.setIsDefault(false);
                    addressMapper.updateByPrimaryKey(address);
                }
            }
        }
        MarketAddress address = new MarketAddress();
        address.setId(addressVo.getId());
        address.setName(addressVo.getName());
        address.setUserId(userId);
        address.setProvince(addressVo.getProvince());
        address.setCity(addressVo.getCity());
        address.setCounty(addressVo.getCounty());
        address.setAddressDetail(addressVo.getAddressDetail());
        address.setAreaCode(addressVo.getAreaCode());
        address.setTel(addressVo.getTel());
        address.setIsDefault(addressVo.isDefaultAddress());
        addressMapper.updateByPrimaryKeySelective(address);
    }
}
